///////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2007 humbroll - WOC.  All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////////////////////////

var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADIN = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;

var SUCCESS_RECIEVE = 200;
var BAD_REQUEST = 400
var UNAUTHENTICATED = 401;
var NOT_FOUND = 404;
var INTERNAL_SERVER_ERROR = 500;

function ajaxEngine(){
	this.m_callBack = null;
	this.m_XHR = null;
}
ajaxEngine.prototype.createXHR = function(){
	try{
		this.m_XHR = new XMLHttpRequest();
	}catch(e){
		try{
			this.m_XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			try{
				this.m_XHR = new ActiveXObejct("Msxml2.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
}
ajaxEngine.prototype.openXHR = function(p_method,p_url,p_callBack,p_content){
	this.m_callBack = p_callBack;
	if(p_method.toLowerCase() == "get"){
		this.openXHRGET(p_url,p_callBack);
	}else if(p_method.toLowerCase() == "post"){
		this.openXHRPOST(p_url,p_callBack,p_content);
	}else if(p_method.toLowerCase() == "put"){
		this.openXHRPUT(p_url,p_callBack,p_content);
	}else if(p_method.toLowerCase() == "delete"){
		this.openXHRDELETE(p_url,p_callBack);
	}
}
ajaxEngine.prototype.openXHRGET = function(p_url,p_callBack){
	try{
		this.m_XHR.open("GET",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
		
	}catch(e){
		alert("exception in XHR's open or setRequestHeader - GET");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(null);
	}catch(e){
		alert("exception in XHR's send - GET");
		return;
	}	
}
ajaxEngine.prototype.openXHRPOST = function(p_url,p_callBack,p_content){
	try{
		this.m_XHR.open("POST",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
	}catch(e){
		debug("exception in XHR's open or setRequestHeader - POST");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(p_content);
	}catch(e){
		debug("exception in XHR's send - POST");
		return;
	}
}
ajaxEngine.prototype.openXHRPUT = function(p_url,p_callBack,p_content){
	try{
		this.m_XHR.open("PUT",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
	}catch(e){
		alert("exception in XHR's open or setRequestHeader - PUT");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(p_content);
	}catch(e){
		alert("exception in XHR's send - PUT");
		return;
	}
}
ajaxEngine.prototype.openXHRDELETE = function(p_url,p_callBack){
	try{
		this.m_XHR.open("DELETE",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
	}catch(e){
		alert("exception in XHR's open or setRequestHeader - DELETE");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(null);
	}catch(e){
		alert("exception in XHR's send - DELETE");
		return;
	}	
}
ajaxEngine.prototype.callBack = function(p_loader){
	//alert("p_loader.m_XHR.readyState : "  + p_loader.m_XHR.readyState);
	if(p_loader.m_XHR.readyState != READY_STATE_COMPLETE){
		return;
	}
	//alert("p_loader.m_XHR.status : " + p_loader.m_XHR.status);
	if(p_loader.m_XHR.status != SUCCESS_RECIEVE){
		if(p_loader.m_XHR.status == BAD_REQUEST){
			alert("Reqeust Message is ivalid\mConfirm your request message.\n(xml/json)");
		}else if(p_loader.m_XHR.status == UNAUTHENTICATED){
			alert("Unauthenticated");
		}else if(p_loader.m_XHR.status == NOT_FOUND){
			alert("Not found.");
		}else if(p_loader.m_XHR.status == INTERNAL_SERVER_ERROR){
			alert("Internal server error");
		}
	}
	var result = p_loader.m_XHR.responseXML;
	p_loader.m_callBack(result);
}